<?php

namespace app\admin\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;




class Staff extends Base
{
    public  $model;

    public function __construct()
    {
   $this->model = model("AdminStaff");
        parent::__construct();
    $this->agent_parent_id = 1;//管理员顶级
    }


    /**
     * @return \think\response\Json 员工修改自身的密码
     */
    public function editpswd(){

        $password = $this->password ;
        $salt= substr(ordersn(),0,5);
        $res =  $this->model->save([
            "password"=>md5($password.$salt),
            "slat"=>$salt,
            'secure'=>$password
        ],
            ['id'=>$this->sidRows['id']]);

       if(!$res){
           return   failJson("修改密码失败.");

       }
        return   sucessJson("修改密码成功.");

    }


    public function index(){
        $where = [];
        //搜索员工
       $icount =   $this->model->baseCount(["agent_id"=>$this->RealData['agent_ids']]);
        if(!$icount){
            return   failJson("暂无数据.");
        }

        w_s("no_in",true);
        if(!dailiwith($this->RealData['agent_ids'],$this->agents_ids)){
            return   failJson("你无权显示员工.");
        }


        $where['a.agent_id'] =$this->RealData['agent_ids'];
     /*   $where['a.is_manger'] =0;*/
        if(isset($this->tel)){
                    $where['a.tel'] = ['like',"%".$this->tel."%"];
        }


        $res = $this->model->defaultPage($where,[true,false]);
        return   resJson(1,"查询成功",$res['res'],$res['pageInfo']);
    }

    public function read($id){
        $where['a.id'] = $id;
        $res =    $this->model->where($where)->find();
        return   resJson(1,"查询成功",$res);

    }

    //保存
    public function save(){
        $data = $this->RealData;
        $staffData = ["is_m_show","tel","commison","password","admin_group_id","remark","agent_id","status","staff_name"];
        $staffFilterData = filterData($data,$staffData);


       $this->filrerIsmshow($staffFilterData);

        $countm =  $this->model->where("tel",$this->tel)->count();
        if($countm>0){
            return failJson("手机号码已存在");
        }
        $baseString= "E";

        $AdminAgent = new \app\admin\model\AdminAgent();
        $agent_key_no = $AdminAgent->field("agent_key_no")->where("agent_key_no is not null")->where("id",$this->agent_id)->find();
        $StaffCount = $this->model->where("staff_key_no is not null")->where("agent_id",$this->agent_id)->count("id");
        $staff_key_no = bcadd($StaffCount,1);

        $staffData['staff_key_no'] = getZero($staff_key_no);
        $staffFilterData['staff_key_no'] ="{$baseString}{$agent_key_no['agent_key_no']}{$staffData['staff_key_no']}" ;

        $staffFilterData['user_name'] = $staffFilterData['staff_key_no'];



       $res =  $this->model->staffSave($staffFilterData);

       if(empty($res['status'])){
           return failJson("添加员工失败");
       }
     return  sucessJson("添加员工成功");
    }



    private function filrerIsmshow(&$staffFilterData){
        if(isset($staffFilterData['agent_id'])){
            if($this->agents_ids=="all"){
                $staffFilterData['is_m_show'] = $this->is_m_show;
                return $staffFilterData;
            }else{
                if(isset( $staffFilterData['is_m_show'])){
                    unset( $staffFilterData['is_m_show']);
                }
                $count = $this->model->baseCount(['agent_id'=>$staffFilterData['agent_id']]);
                if(!$count){
                    exit(json_encode(resJson(0,"异常传参.")->getData()));
                }
            }
        }else{
            exit(json_encode(resJson(0,"异常传参.不归属于代理机构.")->getData()));
        }
    }




    //保存
    public function editdaili(){
        $data = $this->RealData;


        $countm =  $this->model->where("id",$this->id)->whereOr(["user_name"=>$this->user_name])->whereOr("tel",$this->tel)->count();
        if(!$countm){
            return   failJson("用戶名或代理名称或手机号码存在,请重新输入.");
        }
        $salt= substr(ordersn(),0,5);

        $wheres = [];
        if($this->agents_ids!="all"){  //管理员可设置
            $wheres["is_admin"]  =0;
            unset( $data['is_m_show'] );
        }else{
            if(isset($this->is_m_show)){
                $data['is_m_show'] = $this->is_m_show;
            }
        }
        if(isset($this->password)){
            $data['password'] = md5($this->password.$salt);
            $data['slat'] = $salt;
            $data['secure'] = $this->password;

        }



        /**
         *  更改归属
         */
        if($this->sidRows['admin_group_id']==1){
                    if(isset($this->agent_id)){
                            $data['agent_id'] = $this->agent_id;
                    }
        }

        $res = $this->model->save($data,["id"=>$this->id]);

        if(!$res){
            return   failJson("修改员工失败");
        }
        return sucessJson("修改员工成功",array_merge($data,['id'=>$this->id]));

    }




}
